package com.sto.stoapp.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sto.stoapp.StoApplication;
import com.sto.stoapp.activity.R;
import com.sto.stoapp.entity.PictureInfoEntity;
import com.sto.stoapp.status.EnumTableNames;
import com.sto.stoapp.util.UnifiedStaticType;

/**
 * 图片信息
 * 
 * @author hb
 * 
 */
public class PictureInfoAdapter extends BaseAdapter {
	private List<PictureInfoEntity> courierList;

	private Context context;

	/**
	 * 构造函数
	 * 
	 * @param context
	 * @param courierList
	 */
	public PictureInfoAdapter(Context context,
			List<PictureInfoEntity> courierList) {
		this.courierList = courierList;
		this.context = context;
	}

	@Override
	public int getCount() {
		return courierList == null ? 0 : courierList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return courierList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(int position, View view, ViewGroup parent) {
		Holder holder;
		if (view == null) {
			holder = new Holder();
			view = LayoutInflater.from(context).inflate(
					R.layout.sto_picture_adapter, null);
			holder.pictureType = (TextView) view
					.findViewById(R.id.picture_type);
			holder.pictureCode = (TextView) view
					.findViewById(R.id.picture_code);
			holder.pictureDate = (TextView) view
					.findViewById(R.id.picture_date);

			holder.sendStatus = (TextView) view.findViewById(R.id.send_status);
			holder.pictureName = (TextView) view
					.findViewById(R.id.picture_name);
			view.setTag(holder);
		} else {
			holder = (Holder) view.getTag();
		}

		try {
			// if (position == 0) {
			view.setBackgroundResource(R.drawable.btn_selector);
			// } else if (position == (courierList.size() - 1)) {
			// view.setBackgroundResource(R.drawable.btn_selector);
			// } else if (position == (courierList.size() - 2)) {
			// view.setBackgroundResource(R.drawable.btn_selector);
			// } else {
			// view.setBackgroundResource(R.drawable.btn_selector);
			// }

			String type = courierList.get(position).getPictureType();
			if (type.equals(EnumTableNames.STATUS_TABLE_SALESMAN_SIGN.getOpCode())) {
				holder.pictureType.setText("签收");
			} else {
				holder.pictureType.setText("问题");
			}
			holder.pictureCode.setText(courierList.get(position)
					.getPictureCode());
			holder.pictureDate.setText(courierList.get(position)
					.getPictureDate());
			String name = courierList.get(position).getPictureName();

			String status = courierList.get(position).getUploadStatus();
			if (status.equals(UnifiedStaticType.YES_STATUS)) {
				holder.sendStatus.setText(UnifiedStaticType.YES_STATUS);
			} else {
				holder.sendStatus.setText(UnifiedStaticType.NO_STATUS);
			}
			String[] nameBuff = name.split("/");
			if (nameBuff.length > 1) {
				// 如果是585机器执行否则执行else
				if (new Build().MODEL
						.contains(StoApplication.DeviceModel_585)) {
					if (nameBuff.length > 5)
						holder.pictureName.setText(nameBuff[7]);
				} else if (nameBuff.length > 4)
					holder.pictureName.setText(nameBuff[5]);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}

		return view;
	}

	static class Holder {
		// 图片类型
		TextView pictureType;

		// 图片单号
		TextView pictureCode;

		// 图片日期
		TextView pictureDate;

		// 发送状态
		TextView sendStatus;

		// 图片名称
		TextView pictureName;
	}
}
